﻿Public Class perConversao

#Region " Métodos "

    Public Shared Function nuloParaData(ByVal objValor As Object) As Date
        If (objValor Is DBNull.Value) Then
            Return Date.MinValue
        Else
            Return objValor
        End If
    End Function

    Public Shared Function nuloParaZero(ByVal objValor As Object) As Object
        Dim dblValor As Double
        Try
            dblValor = Convert.ToDouble(objValor)
            Return objValor
        Catch
            Return 0
        End Try
    End Function

    Public Shared Function zeroParaNulo(ByVal objValor As Object) As Object
        If objValor = 0 Then
            Return objValor.DBNull.Value
        Else
            Return objValor
        End If
    End Function

    Public Shared Function nuloParaVazio(ByVal objValor As Object) As Object
        If objValor Is DBNull.Value Then
            Return ""
        Else
            Return objValor
        End If
    End Function

    Public Shared Function vazioParaNulo(ByVal objValor As Object) As Object
        If objValor = "" Then
            Return Convert.DBNull
        Else
            Return objValor
        End If
    End Function

    Public Shared Function nuloParaBoleano(ByVal objValor As Object) As Object
        If objValor Is DBNull.Value Then
            Return False
        Else
            Return objValor
        End If
    End Function

    Public Shared Function ToBoolean(ByVal value As Object) As Boolean
        Try
            Return Convert.ToBoolean(nuloParaZero(value))
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function ToByte(ByVal value As Object) As Byte
        Try
            Return Convert.ToByte(nuloParaZero(value))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToChar(ByVal value As Object) As Char
        Return Convert.ToChar(nuloParaZero(value))
    End Function

    Public Shared Function ToDateTime(ByVal value As Object) As Date
        Try
            If value Is Nothing OrElse value Is DBNull.Value Then
                Return Date.MinValue
            Else
                Return Convert.ToDateTime(value)
            End If
        Catch ex As Exception
            Return Date.MinValue
        End Try
    End Function

    Public Shared Function ToDecimal(ByVal value As String) As Decimal
        Try
            If value Is DBNull.Value Then
                value = "0"
            Else
                If value.LastIndexOf(",") > value.LastIndexOf(".") Then
                    value = value.Replace(".", "")
                Else
                    value = value.Replace(",", "").Replace(".", ",")
                End If
            End If
            Return Convert.ToDecimal(nuloParaZero(value.Replace(".", ",")))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToDecimal(ByVal value As Object) As Decimal
        Try
            Return Convert.ToDecimal(nuloParaZero(value))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToDecimal(ByVal value As String, ByVal byCasasDecimais As Byte) As Decimal
        Try
            Return Decimal.Round(Convert.ToDecimal(nuloParaZero(value.Replace(".", ","))), byCasasDecimais)
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToDecimal(ByVal value As Object, ByVal byCasasDecimais As Byte) As Decimal
        Try
            Return Decimal.Round(Convert.ToDecimal(nuloParaZero(value)), byCasasDecimais)
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToDecimal(ByVal value As String, ByVal iCasasDecimais As Integer) As Decimal
        Try
            Return Decimal.Round(Convert.ToDecimal(nuloParaZero(value.Replace(".", ","))), iCasasDecimais)
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToDecimal(ByVal value As Object, ByVal iCasasDecimais As Integer) As Decimal
        Try
            Return Decimal.Round(Convert.ToDecimal(nuloParaZero(value)), iCasasDecimais)
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToDouble(ByVal value As Object) As Double
        Try
            Return Convert.ToDouble(nuloParaZero(value))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToInt32(ByVal value As Object) As Integer
        Try
            Return Convert.ToInt32(nuloParaZero(value))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function ToSingle(ByVal value As Object) As Single
        Try
            Return Convert.ToSingle(nuloParaZero(value))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Shadows Function ToString(ByVal value As Object) As String
        Try
            If value Is Nothing Then
                Return ""
            ElseIf value.GetType.ToString = "System.DateTime" AndAlso value = Date.MinValue Then
                Return Nothing
            Else
                Return Convert.ToString(value)
            End If
        Catch ex As Exception
            Return ""
        End Try
    End Function

#End Region


End Class
